package com.wsz.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsz.pojo.R;
import com.wsz.pojo.Record;
import com.wsz.pojo.UserInfo;
import com.wsz.pojo.vo.RecordVo;
import com.wsz.user.service.RecordService;
import com.wsz.user.mapper.RecordMapper;
import com.wsz.user.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author wangs
* @description 针对表【record】的数据库操作Service实现
* @createDate 2023-02-04 17:23:00
*/
@Service
public class RecordServiceImpl extends ServiceImpl<RecordMapper, Record>
    implements RecordService{

    @Autowired
    private UserInfoService userInfoService;

    @Override
    public R selectList(Long limit, Long page) {

        IPage<Record> recordIPage = new Page<>(page,limit);

        QueryWrapper<Record> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");

        this.page(recordIPage,queryWrapper);

        List<Record> records = recordIPage.getRecords();
        long total = recordIPage.getTotal();
        long current = recordIPage.getCurrent();


        List<RecordVo> recordVoList = new ArrayList<>();

        for(Record record : records){
            UserInfo userInfoA = userInfoService.getById(record.getAId());
            UserInfo userInfoB = userInfoService.getById(record.getBId());

            RecordVo recordVo = new RecordVo();
            recordVo.setA_avatar(userInfoA.getPhoto());
            recordVo.setA_username(userInfoA.getUsername());
            recordVo.setB_avatar(userInfoB.getPhoto());
            recordVo.setB_username(userInfoB.getUsername());
            recordVo.setRecord(record);

            recordVoList.add(recordVo);
        }
        return R.ok().data("records",recordVoList).data("total",total).data("current",current);
    }

    @Override
    public Integer getPkCount(String date) {
        return this.baseMapper.getPkCount(date);
    }
}




